/*
 *   Copyright (C) 2010  Jens Kieselbach
 * 
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 * 
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 *   You may obtain a copy of the License at http://www.gnu.org/licenses/gpl.html
 */

package de.kieselbach.swing

import javax.swing.DefaultButtonModel

class SplitButtonModel extends DefaultButtonModel {

    boolean buttonPressed = false
    SplitButton button

    public SplitButtonModel(SplitButton b) {

        button = b
    }

    @Override
    public void setPressed(boolean b) {

        if (button.mouseInArrowArea || buttonPressed)
            return
        super.setPressed(b)
    }

    public void pressInternal() {

        if ((isPressed()) || !isEnabled()) {
            return
        }

        stateMask |= PRESSED + ARMED

        fireStateChanged()
        buttonPressed = true
    }

    public void releaseInternal() {

        buttonPressed = false
        button.mouseInArrowArea = false
        setArmed(false)
        setPressed(false)
        setRollover(false)
        setSelected(false)
    }

    public boolean isPressedInternal() {

        return buttonPressed
    }

    @Override
    protected void fireStateChanged() {

        if (buttonPressed)
            return
        super.fireStateChanged()
    }

    @Override
    public void setArmed(boolean b) {

        if (buttonPressed)
            return
        super.setArmed(b)
    }

    @Override
    public void setEnabled(boolean b) {

        if (buttonPressed)
            return
        super.setEnabled(b)
    }

    @Override
    public void setSelected(boolean b) {

        if (buttonPressed)
            return
        super.setSelected(b)
    }

    @Override
    public void setRollover(boolean b) {

        if (buttonPressed)
            return
        super.setRollover(b)
    }
}